/*
 * Created by 2006-2-8 16:35:15
 * $Id: BaseDAOImpl.java 76 2006-07-06 17:03:18Z wjx $
 */
package com.someok.base.common;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.orm.ObjectRetrievalFailureException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

/**
 * Base DAO Implement.
 * 
 * <p>
 * <a href="BaseDAOSupport.java.html"><i>View Source</i></a>
 * </p>
 * 
 * @author wjx
 * @version $Revision: 76 $ $Date: 2006-07-07 01:03:18 +0800 $
 */
public class BaseDAOSupport extends HibernateDaoSupport {
	protected Logger logger = Logger.getLogger(getClass());
	
	protected Class entityClass;

	/**
	 * 设置pojo，子类中需要覆盖本方法.
	 * @return
	 */
	protected Class getEntityClass() {
		return entityClass;
	}

    /**
     * Generic method used to get all objects of a particular type. This
     * is the same as lookup up all rows in a table.
     * @param clazz the type of objects (a.k.a. while table) to get data from
     * @return List of populated objects
     */
	public List getAll() {
		return getHibernateTemplate().loadAll(getEntityClass());
	}

    /**
     * Generic method to get an object based on class and identifier. An
     * ObjectRetrievalFailureException Runtime Exception is thrown if
     * nothing is found.
     *
     * @param clazz model class to lookup
     * @param id the identifier (primary key) of the class
     * @return a populated object
     * @see org.springframework.orm.ObjectRetrievalFailureException
     */
	public Object get(Serializable id) {
		Object o = getHibernateTemplate().get(getEntityClass(), id);

		if (o == null) {
			throw new ObjectRetrievalFailureException(getEntityClass(), id);
		}

		return o;
	}

    /**
     * Generic method to save an object - handles both update and insert.
     *
     * @param o the object to save
     */
	public void saveOrUpdate(Object o) {
		getHibernateTemplate().saveOrUpdate(o);
	}
	
	public void saveOrUpdateAll(Collection entities) {
		getHibernateTemplate().saveOrUpdateAll(entities);
	}
	
	public void save(Object o) {
		getHibernateTemplate().save(o);
	}
	
	public void update(Object o) {
		getHibernateTemplate().update(o);
	}

    /**
     * Generic method to delete an object based on class and id.
     *
     * @param clazz model class to lookup
     * @param id the identifier (primary key) of the class
     */
	public void delete(Serializable id) {
		getHibernateTemplate().delete(get(id));
	}

	public List find(String hsql) {
		return getHibernateTemplate().find(hsql);
	}

	public List find(String hsql, Object value) {
		return getHibernateTemplate().find(hsql, value);
	}

	public List find(String hsql, Object[] values) {
		return getHibernateTemplate().find(hsql, values);
	}

}
